package com.isczy.eduservice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.isczy.eduservice.entity.EduVideo;
import com.isczy.eduservice.mapper.EduVideoMapper;
import com.isczy.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import com.isczy.vodapi.VodFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author CZY
 * @since 2021-01-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodFeignService vodFeignService;

    /***
     * ***************************************
     * 功能描述:删除小节
     * @param id
     * @return
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-08 14:01】
     * @author modify:
     */
    @Override
    public void deleteVideoById(String id) {
        //根据小节id获取视频id，调用方法实现视频删除
        String videoSourceId = baseMapper.selectById(id).getVideoSourceId();
        //判断小节里面是否有视频id
        if (StrUtil.isNotEmpty(videoSourceId)){
            DataResult dataResult = vodFeignService.removeAlyVideo(videoSourceId);
            if(dataResult.getCode() == 50000) {
                throw MyException.builder()
                        .code(ResultCode.ERROR)
                        .className(getClass().getName())
                        .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                        .message("删除小节视频失败")
                        .params(videoSourceId)
                        .build();
            }
        }
        baseMapper.deleteById(id);
    }
}
